#include "Cards.h"

// 构造函数

Cards::Cards(const Card& card) {
    m_cards.insert(card);
}

// 添加扑克牌
void Cards::add(const Card& card) {
    m_cards.insert(card);
}

void Cards::add(const Cards& cards) {
    m_cards.insert(cards.m_cards.begin(),cards.m_cards.end());
}

// 操作符重载
Cards& Cards::operator<<(const Card& card) {
    return *this;
}

Cards& Cards::operator<<(const Cards& cards) {
    return *this;
}

// 删除扑克牌
void Cards::remove(const Card& card) {
   m_cards.erase(card);
}

void Cards::remove(const Cards& cards) {
    for (const auto& element : cards.m_cards) {
        // 查找元素并删除
        auto it = m_cards.find(element);
        if (it != m_cards.end()) {
            m_cards.erase(it);
        }
    }
}

// 扑克牌的数量
int Cards::cardCount() const {
    return m_cards.size();
}

// 是否为空
bool Cards::isEmpty() const {
    return m_cards.empty();
}

// 清空扑克牌
void Cards::clear() {
    m_cards.clear();
}

Card Cards::takeRandomCard()
{
    if(!isEmpty()) {
        int num = generateRandomNumber(1, m_cards.size());
        auto it = m_cards.begin();
        for(int i=1; i<num && it != m_cards.end(); ++i, ++it);
        Card card = *it;
        m_cards.erase(it);
        return card;
    }
}

std::vector<Card> Cards::toCardList(Cards::SortType type = Cards::SortType::Desc)const {
    std::vector<Card> cardList(m_cards.begin(), m_cards.end());
    if (type != NoSort) {
        std::sort(cardList.begin(), cardList.end(), 
            [type](const Card& a, const Card& b) {
                return type == Asc ? a.getRank() < b.getRank() : a.getRank() > b.getRank();
            });
    }
    return cardList;
}